package com.amway.qy.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amway.qy.core.constants.QyWechatUrls;
import com.amway.qy.core.service.QyAccessTokenService;
import com.amway.qy.core.service.QyWechatPayService;
import com.amway.qy.util.common.QyHttpClientUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class QyWechatPayServiceImpl implements QyWechatPayService {

	private static final Logger logger = LoggerFactory.getLogger(QyWechatPayServiceImpl.class);
	
	private QyAccessTokenService accessTokenManager;
	
	// Construct here
	public QyWechatPayServiceImpl(QyAccessTokenService accessTokenManager) {
		this.accessTokenManager = accessTokenManager;
	}
	
	@Override
	public QyAccessTokenService getQyAccessTokenService() {
		return this.accessTokenManager;
	}

	@Override
	public void setQyAccessTokenService(QyAccessTokenService tokenService) {
		this.accessTokenManager = tokenService;
	}

	@Override
	public String getJSTicket() {
		List<NameValuePair> paramList = new ArrayList<NameValuePair>();
		paramList.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		String ticketJson = QyHttpClientUtils.httpsGet(QyWechatUrls.OVERWRITE_JSAPI_TICKET, paramList);
		
		logger.info("Return From Wechat TicketJson is >> " + ticketJson);
		JsonObject jsonObj = new JsonParser().parse(ticketJson).getAsJsonObject();
		String ticket = jsonObj.get("ticket").getAsString();
		
		return ticket;
	}

}
